﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public  class U4008RecordDAL
    {
        public static int Add(U4008RecordInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_Add", obj.SerialNumber.ToUpper().Trim(), obj.CallDate, obj.WorkAddress, obj.WorkHours, obj.Problemdescription, obj.CustomerTel, obj.UserName, obj.AgentName,obj.URType);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int U4RID)
        {
            try
            {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_Delete", U4RID);
            DB.ExecuteScalar(dbCommand);
            return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetByU4RID(int U4RID)
	    {
          
                Database DB = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_GetByU4RID", U4RID);
                return DB.ExecuteReader(dbCommand);           
	    }
        public static DataTable GetPagination(string URType, string RealName, string SerialNumber, string CustomerName, string AgentName, int IsComplete,string CallDateFrom, string CallDateTo, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_GetPagination");
            DB.AddInParameter(dbCommand, "URType", DbType.String, URType);
            DB.AddInParameter(dbCommand, "RealName", DbType.String, RealName);
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String,SerialNumber);
            DB.AddInParameter(dbCommand, "CustomerName", DbType.String,CustomerName);
            DB.AddInParameter(dbCommand, "AgentName", DbType.String,AgentName);
            DB.AddInParameter(dbCommand, "IsComplete", DbType.Int16, IsComplete);
            DB.AddInParameter(dbCommand, "CallDateFrom", DbType.String,CallDateFrom);
            DB.AddInParameter(dbCommand, "CallDateTo", DbType.String,CallDateTo);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(U4008RecordInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_Update",obj.U4RID,obj.WorkAddress,obj.WorkHours,obj.Problemdescription,obj.CustomerTel,obj.UserName);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        public static bool UpdateReplay(int U4RID, string ReplayRemark, bool IsComplete)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_UpdateReplay", U4RID, ReplayRemark, IsComplete);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取技师处理统计报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetToJiReport(string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("U4008Record_GetToJiReport", fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables;
        }
       
    }
}
